<!DOCTYPE html>
<html>
<head>
    <title>Thread Pool Monitor</title>
    <link rel="stylesheet" th:href="@{/css/sidebar.css}" />
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex; /* 使用 flex 布局 */
        }

        #content-container {
            flex: 1;
            margin-left: 20px;
        }

        table {
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
        }
    </style>
</head>
<body>
<div th:replace="sidebar.html :: sidebar"></div>
<div id="content-container">
    <button type="button" onclick="goBack()" style="margin-bottom: 20px">Go back</button>

    <table id="threadPoolTable">
        <thead>
        <tr>
            <th>线程池名称</th>
            <th>核心参数</th>
            <th>任务数</th>
            <th>耗时</th>
            <th>队列名称</th>
            <th>拒绝策略</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

<script>
    function goBack() {
        window.location.href = "/index";
    }

    function queryThreadPool() {
        console.log('queryThreadPool')
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "/monitor/threadPool", true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var response = JSON.parse(xhr.responseText);
                var threadPools = response.data;
                var tableBody = document.querySelector("#threadPoolTable tbody");
                threadPools.forEach(function(pool) {
                    var row = document.createElement("tr");
                    row.innerHTML = "<td>" + pool.poolName + "</td>" +
                        "<td>核心线程数：" + pool.coreSize + "<br>最大线程数：" + pool.maxSize + "<br>当前线程数："+ pool.threadCount + "</td>" +
                        "<td>总任务数：" + pool.totalTaskCount + "<br>已完成任务数：" + pool.completedTaskCount + "</td>" +
                        "<td>最小耗时：" + pool.minCostTime + "<br>最大耗时：" + pool.maxCostTime + "<br>平均耗时：" + pool.averageCostTime + "</td>" +
                        "<td>" + pool.queueName + "</td>" +
                        "<td>" + pool.rejectedName + "</td>";
                    tableBody.appendChild(row);
                });
            }
        };
        xhr.send();
    }

    queryThreadPool();
</script>
</body>
</html>